package com.uniview.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.uniview.dao.RegionManagerDao;
import com.uniview.entity.po.RegionManager;
import com.uniview.entity.vo.Pager;
import com.uniview.service.RegionManagerService;

@Service("regionManagerService")
public class RegionManagerServiceImpl implements RegionManagerService {

	@Autowired
	private RegionManagerDao regionManagerDao;

	@Override
	public Integer saveRegion(RegionManager rm) throws Exception {
		return (Integer) regionManagerDao.save(rm);
	}

	@Override
	public boolean updateRegion(RegionManager rm) throws Exception {
		return regionManagerDao.update(rm);
	}

	@Override
	public boolean deleteRegion(Integer Id) throws Exception {
		return regionManagerDao.deleteById(Id);
	}

	@Override
	public Pager<RegionManager> findRegionByPage(String regionName, int pageSize, int pageNo) throws Exception {
		String hql = "";
		if (StringUtils.isNotBlank(regionName)) {
			hql = "from RegionManager rm where rm.regionName like '%" + regionName + "%' order by Id Desc";
		} else {
			hql = "from RegionManager rm order by Id Desc";
		}

		return regionManagerDao.findPagerByQuery(pageNo, pageSize, hql);
	}

	@Override
	public Pager<RegionManager> findRegionByPageBySingle(String regionName, int pageSize, int pageNo) throws Exception {
		String sql = "";
		String countSql = "";
		if (StringUtils.isNotBlank(regionName)) {
			sql = "select * from sd_regionmanager where id in (select min(id) from sd_regionmanager group by regionname) and regionname like '%"
					+ regionName + "%' order by Id Desc";
			countSql = "select count(1) from sd_regionmanager where id in (select min(id) from sd_regionmanager group by regionname) and regionname like '%"
					+ regionName + "%'";
		} else {
			sql = "select * from sd_regionmanager where id in (select min(id) from sd_regionmanager group by regionname) order by Id Desc";
			countSql = "select count(1) from sd_regionmanager where id in (select min(id) from sd_regionmanager group by regionname)";
		}
		Pager<RegionManager> pager = new Pager<RegionManager>();
		List<RegionManager> list = regionManagerDao.findPagerBySql(sql, pageNo, pageSize);
		int totalRow = regionManagerDao.findTotalRowBySql(countSql);
		pager.setPagerList(list);
		pager.setTotalRow(totalRow);
		return pager;
	}

	@Override
	public RegionManager findRegionManager(Integer Id) throws Exception {
		return regionManagerDao.find(Id);
	}

	@Override
	public RegionManager findRegionManagerByWorkNo(String workNo) {

		String hql = "from RegionManager rm where UPPER(rm.workNo) = '" + workNo.toUpperCase() + "'";
		try {
			List<RegionManager> rmList = regionManagerDao.findByHql(hql);
			if (rmList != null && rmList.size() > 0) {
				return rmList.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public RegionManager findRegionManagerByRegionName(String regionName) throws Exception {
		String hql = "from RegionManager rm where rm.regionName = '" + regionName + "'";
		List<RegionManager> rmList = regionManagerDao.findByHql(hql);
		if (null != rmList && !rmList.isEmpty()) {
			return rmList.get(0);
		}
		return null;
	}

	@Override
	public List<RegionManager> findAllRegion(String regionName) throws Exception {
		String hql = "";
		if (StringUtils.isNotBlank(regionName)) {
			hql = "from RegionManager rm where rm.regionName like '%" + regionName + "%' order by Id Desc";
		} else {
			hql = "from RegionManager rm order by Id Desc";
		}
		return regionManagerDao.findByHql(hql);
	}

}
